package com.filldream.fastboot.base.request.pc.database;

import com.filldream.fastboot.common.entity.ErrorCode;
import com.filldream.fastboot.common.util.Assert;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;

/**
 * @author RickSun
 * @version 1.0
 * @data 2021-09-20 10:19
 * @description DownCodeSTO
 */
@Slf4j
@Data
public class DownCodeSTO {

    @ApiModelProperty(value = "表名，多个用空格隔开")
    private String tableNames;

    @ApiModelProperty(value = "作者")
    private String author;

    @ApiModelProperty(value = "包名")
    private String packageName;

    @ApiModelProperty(value = "数据源 为空表示默认数据源，数据源请参考配置文件")
    private String dbNum = "";

    @ApiModelProperty(value = "是否覆盖xml")
    private Boolean isOverWriteXml = true;

    @ApiModelProperty(value = "是否覆盖其他文件")
    private Boolean isOverWriteOther = true;

    @ApiModelProperty(value = "权限token")
    private String token;

    public void check(){
        Assert.isEmpty(tableNames, ErrorCode.PARAM_ERR.reMsg("表名不能为空"));
        Assert.isEmpty(author, ErrorCode.PARAM_ERR.reMsg("作者不能为空"));
        Assert.isEmpty(packageName, ErrorCode.PARAM_ERR.reMsg("包名不能为空"));
        Assert.isEmpty(token, ErrorCode.NO_LOGIN.reMsg());
        if( isOverWriteXml == null ){
            this.isOverWriteXml = true;
        }
        if( isOverWriteOther == null ){
            this.isOverWriteOther = true;
        }

    }

    public String getP(){
        check();
        log.info("---->"+tableNames);
        String[] split = tableNames.split("\\$");
        String result = "";
        for (int i = 0; i < split.length; i++) {
            String temp = split[i];
            if(ObjectUtils.isNotEmpty(temp) && !temp.trim().equals("\\$")){
                result += ( temp.trim() + ( i==split.length-1 ? "" : ",") );
            }
        }
        result = result.trim().replaceAll("\"","").replaceAll(",\"","");
        Assert.checkBoolean(StringUtils.isNotBlank(result) && !result.equals(","),ErrorCode.PARAM_ERR.reMsg("表名错误"));
        return result;
    }

}
